﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetFramework.Cache
{
    public sealed class CacheManager
    {
        #region [  Fileds  ]
        /// <summary>
        /// Total seconds of one day.
        /// </summary>
        public const int DAY_SENONDS = 86400;
        /// <summary>
        /// Total seconds of one hour.
        /// </summary>
        public const int HOUR_SECONDS = 3600;
        /// <summary>
        /// Total seconds of one minute.
        /// </summary>
        public const int MINUTE_SECONDS = 60;
        #endregion

        #region [  Constructors  ]
        private CacheManager()
        {
        }

        static CacheManager() 
        {
            LoadProvider();
        }
        #endregion

        #region [  LoadCacheProvider  ]

        private readonly static object syncObj = new object();
        private static ICacheProvider provider;

        private static void LoadProvider()
        {
            if (provider == null)
            {
                lock (syncObj)
                {
                    if (provider == null)
                    {
                        provider = new AspNetCacheProvider();
                    }
                }
            }
        }

        #endregion

        #region [  Insert  ]

        public static void Insert<T>(string key, T obj)
        {
            if (obj != null)
            {
                provider.Insert(key, obj);
            }
        }

        public static void Insert<T>(string key, T obj, int seconds)
        {
            if (obj != null)
            {
                provider.Insert(key, obj, seconds);
            }
        }

        public static void Insert<T>(string key, T obj, CacheDependency dep)
        {
            if (obj != null)
            {
                provider.Insert(key, obj, dep);
            }
        }
        #endregion

        #region [  Remove  ]
        /// <summary>
        /// Removes the specified Key from the cache
        /// </summary>
        /// <param name="Key"></param>
        public static void Remove(string key)
        {
            provider.Remove(key);
        }
        #endregion

        #region  [  Retrieves  ]
        /// <summary>
        /// Retrieves the specified item from the cache
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>(string key)
        {
            object cacheItem = provider.Get(key);
            if (cacheItem != null)
            {
                return (T)cacheItem;
            }

            return default(T);
        }
        #endregion
    }
}
